<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Session;
use think\captcha;

class Register extends Base
{
	public function __construct(){
		parent::__construct();
	}

	public function index() {
        return $this->fetch();
	}

  // 注册用户
  	public function regist(){
      if ($this->request->isPost()){
      $data=$this->request->only(['username','email','password'],'post');
      $repassword['repassword'] = input('repassword');
      if($data['username'] && $data['email'] && $data['password'] &&$repassword['repassword']) {
        if($data['password'] != $repassword['repassword']){
          return $this->error('两次密码不一致！',url('admin/admin/regist'));
        }
        $data['password']=$this->md5Pwd($data['password']);
        if($this->saveUser($data)){
          $userinfo=$this->user->where('username',$data['username']) -> where('status','1')->find();
          Session::set('userinfo',$userinfo);
          return $this->success('注册成功',url('admin/index/index'));
        }else{
          return $this->error('注册失败，用户存在',url('admin/admin/regist'));
        }
      }else {
          return $this->error('不能存在空字段！',url('admin/admin/regist'));
      }
      return $this->fetch();
    }
  }

}